package vn.com.bookstore;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerPNames;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

import android.util.Log;

public class ObjectNetwork {
	public static final String TAG = "ObjectNetwork";
	public static final int BUFFER_SIZE = 8192;
	public static final int HTTP_CONNECTION_TIMEOUT = 10000;
	public static final int HTTP_STATUS_OK = 200;
	public static final int FILE_PATH_ERROR = 300;
	public static final int FILE_EXIST = 400;
	public static final int SUCCESS = 0;
	
	public static final int CONN_TIMEOUT = 3000;
	public static final int SOCKET_TIMEOUT = 5000;

	protected String inputStreamToString(InputStream is) {

		String line = "";
		StringBuilder total = new StringBuilder();

		// Wrap a BufferedReader around the InputStream
		BufferedReader rd = new BufferedReader(new InputStreamReader(is));

		try {
			// Read response until the end
			while ((line = rd.readLine()) != null) {
				total.append(line);
			}
		} catch (IOException e) {
			Log.e(TAG, e.getLocalizedMessage(), e);
		}

		// Return full string
		return total.toString();
	}

	protected String makeGetParams(ArrayList<NameValuePair> params) {

		if (null == params || params.size() == 0) {
			return "";
		}
		String temp = "";

		if (params != null && params.size() != 0) {
			temp += '?';
			for (NameValuePair nv : params) {
				// Only Value needs to be encorded
				temp += nv.getName();
				temp += '=';

				try {
					temp += URLEncoder.encode(nv.getValue(), "utf-8");
				} catch (UnsupportedEncodingException e) {
					Log.e(TAG, "makeGetParams");
				}

				temp += '&';
			}
			temp = temp.substring(0, temp.length() - 1);
		}
		Log.d(TAG, "MakeGetParams : " + temp);
		return temp;
	}
	
	public DefaultHttpClient getHttpClient() {
		SchemeRegistry schemeRegistry = new SchemeRegistry();
		schemeRegistry.register(new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80));
		// schemeRegistry.register(new Scheme("https", new
		// EasySSLSocketFactory(), 443));
		HttpParams mHttpParams = null;
		;
		if (null == mHttpParams) {
			mHttpParams = new BasicHttpParams();
			mHttpParams.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS,
					30);
			mHttpParams.setParameter(
					ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE,
					new ConnPerRouteBean(30));
			mHttpParams.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
					false);
			HttpProtocolParams.setVersion(mHttpParams, HttpVersion.HTTP_1_1);
			HttpConnectionParams.setConnectionTimeout(mHttpParams,
					HTTP_CONNECTION_TIMEOUT); // agent setting
			// 추가
			HttpConnectionParams.setSoTimeout(mHttpParams,
					HTTP_CONNECTION_TIMEOUT); //
		}
		ClientConnectionManager cm = new SingleClientConnManager(mHttpParams,
				schemeRegistry);
		DefaultHttpClient client = new DefaultHttpClient(cm, mHttpParams);
		return client;
	}
}
